//
//  WHPhotoSelectTool.m
//  BinFengExpressOwner
//
//  Created by 邓伟浩 on 2018/10/27.
//  Copyright © 2018年 BinFeng. All rights reserved.
//

#import "WHPhotoSelectTool.h"
#import <AVFoundation/AVFoundation.h>

@implementation WHPhotoSelectTool

+ (void)showImagePickedAtViewController:(UIViewController <UINavigationControllerDelegate, UIImagePickerControllerDelegate>*)viewController {
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.delegate = viewController;
    imagePicker.allowsEditing = YES;
    imagePicker.navigationBar.translucent = NO;
    
    UIAlertController *theAlert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction *phontoSelectAction = [UIAlertAction actionWithTitle:@"手机相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
            imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            [viewController presentViewController:imagePicker animated:YES completion:nil];
            
        }
        
    }];
    UIAlertAction *cameraAction = [UIAlertAction actionWithTitle:@"立即拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            //相机权限
            AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
            switch (authStatus) {
                case AVAuthorizationStatusNotDetermined: {
                    [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
                        dispatch_async(dispatch_get_main_queue(), ^{
                            if (granted) {
                                imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
                                [viewController presentViewController:imagePicker animated:YES completion:nil];
                            } else {
                                [WHView showAlertwithTitle:nil message:@"已拒绝授权,打开相机失败!" okAction:nil cancelAction:nil];
                            }
                        });
                    }];
                }
                    break;
                case AVAuthorizationStatusAuthorized: {
                    imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
                    [viewController presentViewController:imagePicker animated:YES completion:nil];
                }
                    break;
                    
                case AVAuthorizationStatusDenied:
                case AVAuthorizationStatusRestricted: {
                    [WHView showAlertwithTitle:@"用户拒绝授权" message:@"请去-> [设置 - 隐私 - 相机 - 松鼠侠] 打开访问开关" okTitle:@"去设置" okAction:^(UIAlertAction *action) {
                        NSURL *settingURL = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
                        if ([[UIApplication sharedApplication] canOpenURL:settingURL]) {
                            [[UIApplication sharedApplication] openURL:settingURL];
                        }
                    } cacelTitle:@"取消" cancelAction:nil];
                    return;
                }
                    break;
                default:
                    break;
            }
            
        } else {
            [WHView showMessage:@"不支持拍照" inView:viewController.view];
        }
    }];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [theAlert addAction:cameraAction];
    [cameraAction setValue:CFontTitleBlackColor forKey:@"_titleTextColor"];
    [theAlert addAction:phontoSelectAction];
    [phontoSelectAction setValue:CFontTitleBlackColor forKey:@"_titleTextColor"];
    
    [theAlert addAction:cancelAction];
    [cancelAction setValue:CMDarkOrangeColor forKey:@"_titleTextColor"];
    [viewController presentViewController:theAlert animated:YES completion:nil];
}

@end
